home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 23 / AMIGAplus Sonderheft 23 (2000)(Falke)(DE)[!].iso / Updates / Librarys / zee_ini_library / Sources / C / INI_Example / INI_Example.c < prev    next >
C/C++ Source or Header  |  1999-10-30  |  13KB  |  540 lines

  1. /************************************************
  2. ***  Complex example program for INI library  ***
  3. ***          © 1999 by Basty/Seasons          ***
  4. ***   This program demonstrates the powerful  ***
  5. ***       functions of the INI library        ***
  6. ***               C/C++ version               ***
  7. ***  StormC 3.0 DSK was used for development  ***
  8. ************************************************/
  9.  
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. #include <exec/types.h>
  15. #include <exec/libraries.h>
  16. #include <exec/lists.h>
  17. #include <exec/nodes.h>
  18. #include <libraries/dosextens.h>
  19. #include <libraries/ini_lib.h>
  20. #include <proto/exec.h>
  21. #include <proto/intuition.h>
  22. #include <proto/graphics.h>
  23. #include <proto/ini.h>
  24. #include <intuition/intuitionbase.h>
  25. #include <intuition/screens.h>
  26. #include <intuition/intuition.h>
  27. #include <graphics/rastport.h>
  28.  
  29. #include "INI_Example.h"
  30.  
  31. #define DefaultScrTitle "Example INI Test Screen © 1999 by Basty/Seasons"
  32. #define DefaultWinTitle "Example INI Test Window © 1999 by Basty/Seasons"
  33.  
  34. struct INIScreen
  35. {
  36.   struct MinList iniscr_ScrList;
  37. };
  38.  
  39. struct ScreenList
  40. {
  41.   struct MinNode iniscr_MinNode;
  42.   struct Screen *iniscr_ScreenPtr;
  43.   struct MinList iniscr_WinList;
  44. };
  45.  
  46. struct ScreenWinList
  47. {
  48.   struct MinNode scrwin_MinNode;
  49.   struct Window *scrwin_WindowPtr;
  50. };
  51.  
  52. struct iniLibBase *INIBase;
  53. struct IntuitionBase *IntuitionBase;
  54. struct GfxBase *GfxBase;
  55. struct iniFile *DefINIFile;
  56. struct ScreenList WBScreen =
  57. {
  58.   {NULL, NULL},
  59.   NULL,
  60.   {(struct MinNode *) &(WBScreen.iniscr_WinList.mlh_Tail),NULL,(struct MinNode *) &(WBScreen.iniscr_WinList.mlh_Head)}
  61. };
  62.  
  63. struct INIScreen Screens =
  64. {
  65.   {(struct MinNode *) &(WBScreen.iniscr_MinNode.mln_Succ),NULL,(struct MinNode *) &(WBScreen.iniscr_MinNode.mln_Succ)}
  66. };
  67.  
  68. UWORD DefaultColTab[256] =
  69. {
  70.   0x000, 0xFFF, 0x777, 0xCCC,
  71.   0x444, 0x555, 0x666, 0x888,
  72.   0x111, 0x222, 0xAAA, 0x333,
  73.   0x999, 0xDDD, 0xEEE, 0xBBB
  74. };
  75.  
  76. struct NewScreen NewScreenStr =
  77. {
  78.   0, 0, /* LeftEdge, TopEdge */
  79.   640, 256, /* Width, Height */
  80.   4, /* Depth */
  81.   1, 0, /* DetailPen, BlockPen */
  82.   0x8000, /* ViewModes */
  83.   CUSTOMSCREEN, /* Type */
  84.   0, 0, /* TextAttr, DefaultTitle, */
  85.   0, 0 /* Gadgets, BitMap */
  86. };
  87.  
  88. struct NewWindow NewWindowStr =
  89. {
  90.   0, 0, /* LeftEdge, TopEdge */
  91.   640, 256, /* Width, Height */
  92.   1, 0, /* DetailPen, BlockPen */
  93.   0x200, 0x100F, /* IDCMPFlags, Flags */
  94.   0, 0, /* Gadget, Image */
  95.   0, 0, 0, /* Title, Screen, BitMap */
  96.   0, 0, /* MinWidth, MinHeight */
  97.   640, 256, /* MaxWidth, MaxHeight */
  98.   CUSTOMSCREEN /* Type */
  99. };
  100.  
  101. unsigned char ScreenContext[16] = "Screen";
  102. unsigned char WindowContext[16] = "Window";
  103.  
  104. void InitNode (struct List *NodeList)
  105. {
  106.   NodeList->lh_Head = (struct Node *) &(NodeList->lh_Tail);
  107.   NodeList->lh_Tail = (struct Node *) NULL;
  108.   NodeList->lh_TailPred = (struct Node *) &(NodeList->lh_Head);
  109. }
  110.  
  111. int ReadINIScreens (struct iniFile *iniFile)
  112. {
  113.   struct iniContext *CurrentContext;
  114.   struct ScreenList *ScrStrBuf;
  115.   struct Screen *ScreenStr;
  116.   unsigned long ScrNumCols;
  117.   UWORD ScrColorTable[256];
  118.  
  119.   /* Set FAIL error mark */
  120.  
  121.   int rc = 20;
  122.  
  123.   /* Start with first screen */
  124.  
  125.   unsigned long CurrentScrNum = 1;
  126.  
  127.   for (;;)
  128.   {
  129.  
  130.     /* Convert screen number to ASCII using standard decimal format */
  131.  
  132.     iniIntToStr ( ScreenContext+6, CurrentScrNum, INI_FORMAT_DEC, 0L, ' ');
  133.  
  134.     /* Increment screen counter */
  135.  
  136.     CurrentScrNum++;
  137.  
  138.     /* Search for context and check if found */
  139.  
  140.     if (!(CurrentContext = iniFindContext ( iniFile, ScreenContext, 0L)))
  141.     {
  142.       /* Found? No -> end of search */
  143.       rc = 0;
  144.       break;
  145.     }
  146.  
  147.     /* Get left edge value or use default value of zero. */
  148.  
  149.     NewScreenStr.LeftEdge = iniReadLong ( iniFile, ScreenContext,
  150.       "LeftEdge", 0L, 0L);
  151.  
  152.     /* Get top edge value or use default value of zero. */
  153.  
  154.     NewScreenStr.TopEdge = iniReadLong ( iniFile, ScreenContext,
  155.       "TopEdge", 0L, 0L);
  156.  
  157.     /* Get width value or default width of 640 pixels. */
  158.  
  159.     NewScreenStr.Width = iniReadLong ( iniFile, ScreenContext,
  160.       "Width", 640L, 0L);
  161.  
  162.     /* Get height value or default height of 256 pixels. */
  163.  
  164.     NewScreenStr.Height = iniReadLong ( iniFile, ScreenContext,
  165.       "Height", 256L, 0L);
  166.  
  167.     /* Get depth value or default of 4 bitplanes (16 colors). */
  168.  
  169.     NewScreenStr.Depth = iniReadLong ( iniFile, ScreenContext,
  170.       "Depth", 4L, 0L);
  171.  
  172.     /* Get view modes value or default view mode of HIRES (0x8000). */
  173.  
  174.     NewScreenStr.ViewModes = iniReadLong ( iniFile, ScreenContext,
  175.       "ViewModes", 0x8000L, 0L);
  176.  
  177.     /* Get screen default title or use default string */
  178.  
  179.     NewScreenStr.DefaultTitle = iniReadStr ( iniFile, ScreenContext,
  180.       "Title", DefaultScrTitle, 0L);
  181.  
  182.     /* Copy all 256 default colors to updated color table */
  183.  
  184.     memcpy (ScrColorTable,DefaultColTab,256);
  185.     ScrNumCols = (NewScreenStr.Depth < 8 ? 1L<<NewScreenStr.Depth : 256L);
  186.  
  187.     /* Read all necessary color table entries into color table */
  188.  
  189.     iniReadWordA ( iniFile, ScreenContext, "ColorTable", (WORD *) &ScrColorTable,
  190.       ScrNumCols, 0L);
  191.  
  192.     /* We need storage buffers for our screens */
  193.  
  194.     if (!(ScrStrBuf = (struct ScreenList *) iniAllocPMem (sizeof(struct ScreenList))))
  195.     {
  196.       /* Deallocate name string buffer. Don't forget ! */
  197.  
  198.       iniFreeNameStr (NewScreenStr.DefaultTitle);
  199.       break;
  200.     }
  201.  
  202.     /* Add them to list */
  203.  
  204.     AddTail ((struct List *) &Screens.iniscr_ScrList, (struct Node *) ScrStrBuf);
  205.  
  206.     /* Open the screen */
  207.  
  208.     ScreenStr = OpenScreen (&NewScreenStr);
  209.  
  210.     ScrStrBuf->iniscr_ScreenPtr = ScreenStr;
  211.  
  212.     /* Initialize window node list */
  213.  
  214.     InitNode ((struct List *) &(ScrStrBuf->iniscr_WinList));
  215.  
  216.     if (ScreenStr == NULL)
  217.     {
  218.       /* Deallocate name string buffer. Don't forget */
  219.  
  220.       iniFreeNameStr (NewScreenStr.DefaultTitle);
  221.       break;
  222.     }
  223.  
  224.     LoadRGB4 (&ScreenStr->ViewPort, ScrColorTable, ScrNumCols);
  225.   }
  226.  
  227.   return ( rc );
  228. }
  229.  
  230. int ReadINIWindows (struct iniFile *iniFile)
  231. {
  232.   struct iniContext *CurrentContext;
  233.   struct ScreenWinList *WinStrBuf;
  234.   struct Window *WindowStr;
  235.   struct ScreenList *scr;
  236.   unsigned long ScreenNum,i;
  237.  
  238.   /* Set FAIL error mark */
  239.  
  240.   int rc = 20;
  241.  
  242.   /* Start with first window */
  243.  
  244.   unsigned long CurrentWinNum = 1;
  245.  
  246.   for (;;)
  247.   {
  248.  
  249.     /* Convert window number to ASCII using standard decimal format */
  250.  
  251.     iniIntToStr ( WindowContext+6, CurrentWinNum, INI_FORMAT_DEC, 0L, ' ');
  252.  
  253.     /* Increment window counter */
  254.  
  255.     CurrentWinNum++;
  256.  
  257.     /* Search for context and check if found */
  258.  
  259.     if (!(CurrentContext = iniFindContext ( iniFile, WindowContext, 0L)))
  260.     {
  261.       /* Found? No -> end of search */
  262.       rc = 0;
  263.       break;
  264.     }
  265.  
  266.     /* Get left edge value or use default value of zero. */
  267.  
  268.     NewWindowStr.LeftEdge = iniReadLong ( iniFile, WindowContext,
  269.       "LeftEdge", 0L, 0L);
  270.  
  271.     /* Get top edge value or use default value of zero. */
  272.  
  273.     NewWindowStr.TopEdge = iniReadLong ( iniFile, WindowContext,
  274.       "TopEdge", 0L, 0L);
  275.  
  276.     /* Get width value or default width of 640 pixels. */
  277.  
  278.     NewWindowStr.Width = iniReadLong ( iniFile, WindowContext,
  279.       "Width", 640L, 0L);
  280.  
  281.     /* Get height value or default height of 256 pixels. */
  282.  
  283.     NewWindowStr.Height = iniReadLong ( iniFile, WindowContext,
  284.       "Height", 256L, 0L);
  285.  
  286.     /* Get IDCMP flags or default to 0x20000 (borderless). */
  287.  
  288.     NewWindowStr.IDCMPFlags = iniReadLong ( iniFile, WindowContext,
  289.       "IDCMP", 0x20000, 0L);
  290.  
  291.     /* Get window flags or default to 0x100F (Autoactivate + all gadgets) */
  292.  
  293.     NewWindowStr.Flags = iniReadLong ( iniFile, WindowContext,
  294.       "Flags", 0x100FL, 0L);
  295.  
  296.     /* Get screen default title or use default string */
  297.  
  298.     NewWindowStr.Title = iniReadStr ( iniFile, WindowContext,
  299.       "Title", DefaultWinTitle, 0L);
  300.  
  301.     /* Get min width value or default min width of 32 pixels. */
  302.  
  303.     NewWindowStr.MinWidth = iniReadLong ( iniFile, WindowContext,
  304.       "MinWidth", 32L, 0L);
  305.  
  306.     /* Get max width value or default max width of 640 pixels. */
  307.  
  308.     NewWindowStr.MaxWidth = iniReadLong ( iniFile, WindowContext,
  309.       "MaxWidth", 640L, 0L);
  310.  
  311.     /* Get min height value or default min height of 32 pixels. */
  312.  
  313.     NewWindowStr.MinHeight = iniReadLong ( iniFile, WindowContext,
  314.       "MinHeig